/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serwer;

import Serializable.ParsedCmd;
import java.util.LinkedList;

/**
 *
 * @author Marcin
 */
public class GameInfo {
    public int players[];
    public String playersN[];
    public boolean ready[];
    public boolean loaded[];
    public Receiver pR[];
    
    public long lastTimeProcess=0L;
    
    public LinkedList<ParsedCmd> fromQueue=new LinkedList<>();
    public LinkedList<ParsedCmd> toQueue=new LinkedList<>();
    
    int maxPlayers=0;
    public String gameName = "gra";
    
    public synchronized void process(){
        while(fromQueue.size()!=0){
            ParsedCmd pC=from(null);
            ParsedCmd out=new ParsedCmd();
            out.from=pC.from;
            switch(pC.mainCmd){
                case "LOADED":
                    out.mainCmd="PLOADED";
                    out.intParam=new int[]{pC.intParam[0]};
                    loaded[pC.intParam[0]]=true;
                    to(out);
                    
                    boolean rdyToStart=true;
                    for(int i=0;i<maxPlayers;++i){
                        if(loaded[i]==false){
                            rdyToStart=false;
                        }
                    }
                    if(rdyToStart){
                        ParsedCmd rdy=new ParsedCmd("PLAYGAME");
                        rdy.from=-1;
                        to(rdy);
                    }
                    break;
                case "NMOB":
                    out.mainCmd="XNMOB";
                    out.intParam=new int[]{pC.intParam[0],pC.intParam[1]};
                    out.stringParam=new String[]{pC.stringParam[0]};
                    to(out);
                    break;
                case "DMOB":
                    out.mainCmd="XDMOB";
                    out.intParam=new int[]{pC.intParam[0],pC.intParam[1]};
                    to(out);
                    break;
                case "NTOWER":
                    out.mainCmd="XNTOWER";
                    out.intParam=new int[]{pC.intParam[0],pC.intParam[1]};
                    out.floatParam=pC.floatParam.clone();
                    to(out);
                    break;
                    
            }           
        }
        while(toQueue.size()!=0){
            ParsedCmd pC=to(null);
            for(int i=0;i<maxPlayers;++i){
                if(players[i]!=-1 && pC.from!=players[i]){
                    pR[i].sendQueue(pC);
                }
            }
        }
    }
    
    public synchronized ParsedCmd from(ParsedCmd pC){
        if(pC==null)
            return fromQueue.pop();
        else{
            fromQueue.push(pC);
            return null;
        }
    }
    
    public synchronized ParsedCmd to(ParsedCmd pC){
        if(pC==null)
            return toQueue.pop();
        else{
            toQueue.push(pC);
            return null;
        }
    }
    
    public GameInfo(String name, UserState us,int maxP){
        gameName=name;
        
        maxPlayers=maxP;
        players = new int[maxP];
        playersN = new String[maxP];
        ready = new boolean[maxP];
        loaded = new boolean[maxP];
        pR= new Receiver[maxP];
        
        for(int i=0;i<maxP;++i){
            players[i]= -1;
            playersN[i]= "Czekam...";
            ready[i]=false;
            loaded[i]=false;
            pR[i]=null;
        }
    }
    public int players(){
        int p=0;
        for(int i=0;i<maxPlayers;++i){
            if(players[i]!=-1){
                ++p;
            }
        }
        return p;
    }
    synchronized public int[] get(){
        return players;
    }
    synchronized public String getN(int i){
        return playersN[i];
    }
    
    synchronized public boolean join(UserState us){
        for(int i=0;i<maxPlayers;++i){
            if(players[i]==-1){
                us.IDinGame=i;
                players[i]=us.ID;
                playersN[i]=us.nick;
                pR[i]=us.my;
                ParsedCmd snd = new ParsedCmd("NPLAYER",new int[]{i,maxPlayers},new String[]{us.nick});
                for(int j=0;j<maxPlayers;++j){
                    if(players[j]!=-1){
                        pR[j].send(snd);
                    }
                }
                return true;
            }
        }
        return false;
    }
    synchronized public boolean disconnect(UserState us){
        for(int i=0;i<maxPlayers;++i){
            if(players[i]==us.ID){
                players[i]=-1;
                playersN[i]= "Czekam...";
                ready[i]=false;
                
                ParsedCmd snd = new ParsedCmd("DCPLAYER",new int[]{i});
                for(int j=0;j<maxPlayers;++j){
                    if(players[j]!=-1){
                        pR[j].send(snd);
                    }
                }
                return true;
            }
        }
        return false;
    }
    synchronized public boolean ready(UserState us){
        for(int i=0;i<maxPlayers;++i){
            if(players[i]==us.ID){
                ready[i]=true;
                
                ParsedCmd snd = new ParsedCmd("RDPLAYER",new int[]{i});
                for(int j=0;j<maxPlayers;++j){
                    if(players[j]!=-1){
                        pR[j].sendQueue(snd);
                    }
                }
                for(int j=0;j<maxPlayers;++j){
                    if(ready[j]==false)
                        return true;
                }
                
                snd = new ParsedCmd("STARTGAME");                        
                for(int j=0;j<maxPlayers;++j){
                    if(players[j]!=-1){
                        pR[j].sendQueue(snd);
                    }
                }
                return true;
            }
        }
        return false;
    }
    
    synchronized public boolean notReady(UserState us){
        for(int i=0;i<maxPlayers;++i){
            if(players[i]==us.ID){
                ready[i]=false;
                
                ParsedCmd snd = new ParsedCmd("NRDPLAYER",new int[]{i});
                for(int j=0;j<maxPlayers;++j){
                    if(players[j]!=-1){
                        pR[j].send(snd);
                    }
                }
                return true;
            }
        }
        return false;
    }
    synchronized public boolean canStart(){
        for(int i=0;i<maxPlayers;++i){
            if(players[i]==-1){
                return false;
            }
        }
        return true;
    }
}
